package com.RedRiver.hospitalsystem.personal_service.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.RedRiver.hospitalsystem.personal_service.common.Personal_serviceResultCode;
import com.RedRiver.hospitalsystem.personal_service.common.exception.DepartmentException;
import com.RedRiver.hospitalsystem.personal_service.infra.mapper.DepartmentMapper;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Department;
import com.RedRiver.hospitalsystem.personal_service.infra.model.Doctor;
import com.RedRiver.hospitalsystem.personal_service.service.DepartmentService;
import com.RedRiver.hospitalsystem.personal_service.service.Form.DepartmentChangeForm;
import com.RedRiver.hospitalsystem.personal_service.service.Form.DepartmentForm;
import com.RedRiver.hospitalsystem.personal_service.service.Form.PageForm;
import com.RedRiver.hospitalsystem.personal_service.service.info.DepartmentInfo;
import com.RedRiver.hospitalsystem.personal_service.service.info.DoctorPageInfo;
import com.RedRiver.hospitalsystem.result.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 龙世超
 * @since 2022-04-26
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements DepartmentService {

    @Resource
    private DepartmentMapper departmentMapper;

    /**
     * 分页查询所有部门
     * @param form
     * @return
     */
    @Override
    public IPage<DepartmentInfo> findPageDepartment(PageForm form) {
        Page<Department> departmentInfoPage = new Page<>();
        departmentInfoPage = this.baseMapper.selectPage(new Page<>(form.getPage() == 0 ? 1 : form.getPage(), form.getPageSize() == 0 ? 2 : form.getPageSize()),
                null);
        List<Department> departmentInfoList = departmentInfoPage.getRecords();
        List<DepartmentInfo> departmentPageInfoList = BeanUtil.copyToList(departmentInfoList, DepartmentInfo.class);
        Page<DepartmentInfo> page = new Page<>();
        page.setRecords(departmentPageInfoList);
        page.setCurrent(departmentInfoPage.getCurrent());
        page.setSize(departmentInfoPage.getSize());
        page.setTotal(departmentInfoPage.getTotal());
        return page;
    }

    /**
     * 创建新部门
     * @param departmentForm
     * @return
     */
    @Override
    public Result createDepartment(DepartmentForm departmentForm) {
        Department department_name = departmentMapper.selectOne(new QueryWrapper<Department>().eq("department_name", departmentForm.getDepartmentName()));
        if (department_name!=null) throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_NAME_ERROR);
        Department department = departmentMapper.selectOne(new QueryWrapper<Department>()
                .eq("department_location", departmentForm.getDepartmentLocation())
                .eq("department_state", departmentForm.getDepartmentState()));
        if (department!=null&&department.getDepartmentState()==1) throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_ADDRESS_STATUS);
        int insert = departmentMapper.insert(BeanUtil.copyProperties(departmentForm, Department.class));
        if (insert==0)throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_CREATE_ERROR);
        return Result.getInstance().setMsg("创建部门成功");
    }

    /**
     * 修改部门信息
     * @param departmentChangeForm
     * @return
     */
    @Override
    public Result updateDepartment(DepartmentChangeForm departmentChangeForm) {
        List<Department> departments = departmentMapper.selectList(new QueryWrapper<Department>()
                .eq("department_location", departmentChangeForm.getDepartmentLocation())
                .eq("department_state", departmentChangeForm.getDepartmentState()));
        for (Department department : departments) {
            if (department!=null&&department.getDepartmentState()==1&&department.getDepartmentId()!=departmentChangeForm.getDepartmentId())
                throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_ADDRESS_STATUS);
        }
        int i = departmentMapper.update(BeanUtil.copyProperties(departmentChangeForm, Department.class), new QueryWrapper<Department>().eq("department_id", departmentChangeForm.getDepartmentId()));
        if (i==0)throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_UPDAYE_ERROR);
        return Result.getInstance().setMsg("修改部门成功");
    }

    /**
     * 根据部门id删除部门
     * @param departmentId
     * @return
     */
    @Override
    public Result delDepartment(Integer departmentId) {
        int department_id = departmentMapper.delete(new QueryWrapper<Department>().eq("department_id", departmentId));
        if (department_id==0) throw new DepartmentException(Personal_serviceResultCode.DEPARTMENT_DELETE_ERROR);
        return Result.getInstance().setMsg("删除成功");
    }
}
